<?php

namespace app\api\controller;

use app\admin\model\Message;
use app\admin\model\Notice;
use app\admin\model\Slideshow;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $db_jx;
    protected $link = "https://junxm.com";


    //轮播图
    public function slideshowlist()
    {
        $this->success('请求成功', [
            'slideshow_list' => Slideshow::all(['status' => 'normal']),//轮播图
        ]);
    }

    //信息列表
    public function infolist()
    {
        $limit = $this->request->request('limit') ? $this->request->request('limit') : 10;//长度
        $page = $this->request->request('page');//当前页数
        $type = $this->request->request('type') ? $this->request->request('type') : 1;//类型
        if($type == 1){
            $model = new Notice();
        }else{
            $model = new Message();
        }

        $query = $model
            ->where(['status' => 'normal']);
        $row = $query->limit($limit,$page)->select();
        $total = $query->count();
        $row_count = ceil($total / $limit) ;

        $this->success('返回成功',[
            "total" => $total,
            "row" => $row,
            "row_count" => $row_count,
        ]);

    }

    public function messagelist()
    {
        $limit = $this->request->request('limit') ? $this->request->request('limit') : 10;//长度
        $page = $this->request->request('page');//当前页数

        $this->db_jx = Db::connect('database_jx');
        $where = ['category_id' => 12];
        $query =  $this->db_jx->name('article')->where($where);
        $total = $this->db_jx->name('article')->where($where)->count();

        $row = $query->limit($limit,$page)->order('id desc')->select();

        $row_count = ceil($total / $limit) ;

        foreach ($row as &$item)
        {
            $item['link'] = "{$this->link}/index/index/newsdetail/id/{$item['id']}.html";
        }

        $this->success('返回成功',[
            "total" => $total,
            "row" => $row,
            "row_count" => $row_count,
        ]);
    }

    public function noticelist1()
    {
        $limit = $this->request->request('limit') ? $this->request->request('limit') : 10;//长度
        $page = $this->request->request('page');//当前页数

        $this->db_jx = Db::connect('database_jx');
        $query =  $this->db_jx->name('car_sharing');
        $row = $query->limit($limit,$page)->order('id desc')->select();
        $total = $query->count();
        $row_count = ceil($total / $limit) ;

        foreach ($row as $key => &$item)
        {
            $item['image'] = "https://web-1304091150.cos.ap-shanghai.myqcloud.com/img/7.png";
            $item['link'] = "{$this->link}/index/car/details/id/{$item['id']}.html";
        }

        $this->success('返回成功',[
            "total" => $total,
            "row" => $row,
            "row_count" => $row_count,
        ]);
    }

    public function noticelist2()
    {
        $limit = $this->request->request('limit') ? $this->request->request('limit') : 10;//长度
        $page = $this->request->request('page');//当前页数
        $type = $this->request->request('type') ? $this->request->request('type') : 1;//类型

        $this->db_jx = Db::connect('database_jx');

        $row =  $this->db_jx->name('demand')->alias("d")->join('demand_list dt', 'dt.demand_id = d.id')
            ->where(['d.demtype' => 6])->limit($limit,$page)->order('d.id desc')->field("d.*,dt.carimage")->select();

        $total = $this->db_jx->name('demand')->where(['demtype' => 6])->count();
        $row_count = ceil($total / $limit) ;

        $data = [];
        foreach ($row as $key => &$item)
        {
            $arr = explode(",", $item['carimage']);
            $data[$key]['image'] = $arr[0];
            $data[$key]['title'] = $item['demtitle'];
            $data[$key]['createtime'] = $item['createtime'];
            // $data[$key]['link'] = "{$this->link}/index/chemical/details.html?id={$item['id']}";
            $data[$key]['link'] = "{$this->link}/index/chemical/details/ids/{$item['id']}.html";
        }

        $this->success('返回成功',[
            "total" => $total,
            "row" => $data,
            "row_count" => $row_count,
        ]);
    }


    //信息详情
    public function details()
    {
        $id = $this->request->request('id');//ID
        $type = $this->request->request('type') ? $this->request->request('type') : 1;//类型

        if(!$id){
            $this->error(__('Invalid parameters'));
        }

        $where = ['status' => 'normal'];
        if($type == 1){
            $info = Notice::get($where);
        }else{
            $info = Message::get($where);
        }
        $this->success('请求成功',[
            'info' => $info
        ]);

    }
}